//
//  ECUIVC_OrderDetail.m
//  Edu_iOS2018
//
//  Created by yanyan on 2018/1/16.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECUIVC_OrderDetail.h"
#import "ECData_Format.h"
#import "ECUITVCell_CourseOrder.h"
#import "ECUIVC_Cash.h"
#import "ECUIVC_ToComment.h"
#import "CYDataHelper.h"
#import "ECUIAlertView_CourseDetail.h"
@interface ECUITVCell_OrderDetail()

@end
@implementation ECUITVCell_OrderDetail
@synthesize nameLabel = _nameLabel;
@synthesize briefLabel = _briefLabel;
- (void)addSubviews
{
    [super addSubviews];
    [self.contentView addSubview:self.nameLabel];
 
    
    
}
- (UILabel *)briefLabel
{
    if (!_briefLabel) {
        _briefLabel = [[UILabel alloc]init];
        _briefLabel.font = [UIFont systemFontOfSize:15];
        _briefLabel.textAlignment = NSTextAlignmentLeft;
//        [self.contentView addSubview:_briefLabel];

    }
    return _briefLabel;
}
- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.font = [UIFont systemFontOfSize:15];
        _nameLabel.textColor = [UIColor colorWithHex:0x666666];
    }
    return _nameLabel;
}
@end
//

@implementation ECUITVCell_OrderDetail0
- (void)addSubviews
{
    [super addSubviews];
    [self.contentView addSubview:self.briefLabel];

    self.nameLabel.font = [UIFont systemFontOfSize:15];
    self.nameLabel.textColor = [UIColor colorWithHex:0x666666];
    CGFloat padding = 10;
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(10);
        make.centerY.equalTo(self.contentView);
        make.width.equalTo(@(80));
        make.top.equalTo(self.contentView).offset(5);
        make.bottom.equalTo(self.contentView).offset(-5);

    }];
    [self.briefLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(padding);
        make.centerY.equalTo(self.nameLabel);
        make.right.equalTo(self.contentView).offset(-padding);
    }];
    
}
- (void)setObject:(id)object
{
    [super setObject:object];
    self.nameLabel.text = [object valueForKey:@"name"];
    self.briefLabel.text = [object valueForKey:@"brief"];
    self.briefLabel.textColor = [object valueForKey:@"color"];
}
@end


@implementation ECUITVCell_OrderDetail1
- (void)addSubviews
{
    [super addSubviews];
    [self.contentView addSubview:self.briefLabel];
    self.briefLabel.textAlignment = NSTextAlignmentRight;
    self.briefLabel.textColor = [UIColor colorWithHex:0x666666];
    self.nameLabel.font = [UIFont systemFontOfSize:15];
    self.nameLabel.textColor = [UIColor colorWithHex:0x333333];
    CGFloat padding = 10;
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(10);
        make.centerY.equalTo(self.contentView);
        make.width.equalTo(@(80));
        make.height.greaterThanOrEqualTo(@(30));
        make.top.equalTo(self.contentView).offset(8);
        make.bottom.equalTo(self.contentView).offset(-8);
    }];
    [self.briefLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(padding);
        make.centerY.equalTo(self.nameLabel);
        make.right.equalTo(self.contentView).offset(-padding);
    }];
}
- (void)setObject:(id)object
{
    [super setObject:object];
    self.nameLabel.text = [object valueForKey:@"name"];
    self.briefLabel.text = [object valueForKey:@"brief"];
    if ([object valueForKey:@"color"]) {
        self.briefLabel.textColor = [object valueForKey:@"color"];
    } else {
        self.briefLabel.textColor = [UIColor colorWithHex:0x333333];
    }
}
@end
//
@interface ECUITVCell_OrderDetailCourse()
@property(nonatomic,strong)ECUIView_CourseOrder *courseView;
@end
@implementation ECUITVCell_OrderDetailCourse
-(void)addSubviews
{
    _courseView = [[ECUIView_CourseOrder alloc]init];
    [_courseView setBackgroundColor: [UIColor whiteColor]];
    [self.contentView addSubview:_courseView];
    WeakSelf(weakSelf);
    [_courseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView);
        make.top.equalTo(weakSelf);
        make.width.equalTo(weakSelf.contentView);
        make.height.equalTo(@(kProportion_Height(246)));
    }];
}
- (void)setObject:(id)object
{
    [super setObject:object];
    ECCourseOrderModel *mo = object;
//    _courseView.object = object;
    [self.courseView.courseImageView sd_setImageWithURL:[NSURL URLWithString:mo.mo_course.pictureUrl] placeholderImage:nil];
    self.courseView.courseNameLabel.text = mo.mo_course.title;
    self.courseView.courseTeacherLabel.text = [NSString stringWithFormat:@"任课老师: %@",mo.mo_course.teacher];
    self.courseView.priceLabel.attributedText = [ECFormatHelper attributedStringForPromotionPrice:mo.mo_course.price decimal:2 smallFontSize:10];
}
@end
#import "CYUITVHeaderFooter_base.h"
#import "ECUIVC_CourseDetail.h"
#import "UIImage+RenderedImage.h"

@interface ECUIVC_OrderDetail ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UIButton *bottomBtn;//支付按钮
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,strong)ECCourseOrderModel *orderModel;
@property(nonatomic,strong)ECUIAlertView_CourseDetail *uiview_alert;

@end

@implementation ECUIVC_OrderDetail

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单详情";
    // Do any additional setup after loading the view.
    if (self.object) {
        [self requestOrderDetail:self.object];
    }

}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.shadowImage = [UIImage imageWithColor: [[UIColor darkGrayColor] colorWithAlphaComponent:1] renderSize:CGSizeMake(kScreenWidth, 0.20)];
}
- (void)addBarButtons
{
    [super addBarButtons];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""style:UIBarButtonItemStylePlain target:self action:nil];

}
- (UIButton *)bottomBtn
{
    if (!_bottomBtn) {
        _bottomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _bottomBtn.frame = CGRectMake(0, CGRectGetHeight(self.view.frame)-45, kScreenWidth, 45);
        [_bottomBtn addTarget:self action:@selector(action_bottom:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_bottomBtn];
    }
    return _bottomBtn;
}
-(void)action_bottom:(id)sender{
    ECCourseOrderModel *mo = _orderModel;
    CourseModel *mo_selected = _orderModel.mo_course;
    if ([CYDataHelper isNull:mo_selected.modelId] || mo_selected.deleted) {
        [self showAlertViewWithTitle:kAlertTipOfCourseDelete];
        return;
    }
    if (![mo_selected.status isEqualToString:@"published"]) {
        [self showAlertViewWithTitle:kAlertTipOfCourseClosed];
        return;
    }
    if ([mo.orderState isEqualToString:@"paid"] && !mo.reviewed) {
        ECUIVC_ToComment *uivc_toComment = [[ECUIVC_ToComment alloc]init];
        uivc_toComment.object = mo;
        [self.navigationController pushViewController:uivc_toComment animated:YES];

    } else if ([mo.orderState isEqualToString:@"created"]) {
        ECUIVC_Cash *uivc_pay = [[ECUIVC_Cash alloc]init];
        uivc_pay.object = _orderModel;
        [self.navigationController pushViewController:uivc_pay animated:YES];
    }
   
}
-(void)requestOrderDetail:(ECCourseOrderModel *)mo
{
    WeakSelf(weakSelf);
    [HWHttpTool requestWithMethod:@"GET" url:URL_CourseOrderDetailWithId(mo.orderId) params:nil headers:@{@"X-AUTH-TOKEN":[UserInfoManager shareUser].token} success:^(id json) {
         dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf reloadData:json];
         });
        
    } failure:^(NSError *error) {
        DLog(@"error:%@",error);
    }];
}
-(void)reloadData:(id)obj
{
    _orderModel = [[ECCourseOrderModel alloc]initWithDetailObject:[obj valueForKey:@"data"]];
    ECCourseOrderModel *mo = _orderModel;
        if ([mo.orderState isEqualToString:@"paid"] && !mo.reviewed) {
            self.bottomBtn.hidden = NO;
            [self.bottomBtn setBackgroundColor:[UIColor colorWithHexString:@"#f5f5f5"]];
            [self.bottomBtn setTitle:@"去评价" forState:UIControlStateNormal];
            [self.bottomBtn setTitleColor:ECTHEMESELECTEDCOLOR forState:UIControlStateNormal];
        } else if ([mo.orderState isEqualToString:@"created"]) {
            self.bottomBtn.hidden = NO;
            [self.bottomBtn setBackgroundColor:ECTHEMESELECTEDCOLOR];
            [self.bottomBtn setTitle:@"立即支付" forState:UIControlStateNormal];
            [self.bottomBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        } else {
            self.bottomBtn.hidden = YES;
        }
        if (self.isViewLoaded) {
            [self.dataSource removeAllObjects];
            [self.dataSource addObjectsFromArray:[ECData_Format arrayForOrderDetail:mo]];
            [self.tableView reloadData];
        }
}
- (void)addSubviews
{
    [super addSubviews];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, self.view.frame.size.height - 50) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
   UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    view.backgroundColor = [UIColor whiteColor];
    _tableView.tableHeaderView = view;
    _tableView.tableFooterView = [UIView new];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tableView.backgroundColor = [UIColor whiteColor];
    [_tableView registerClass:[ECUITVCell_OrderDetail0 class] forCellReuseIdentifier:@"cell0"];
    [_tableView registerClass:[ECUITVCell_OrderDetailCourse class] forCellReuseIdentifier:@"cell1"];
    [_tableView registerClass:[ECUITVCell_OrderDetail1 class] forCellReuseIdentifier:@"cell2"];
    [_tableView registerClass:[CYUITVHeaderFooter_base class] forHeaderFooterViewReuseIdentifier:@"footer"];
    [self.view addSubview:_tableView];
}
-(void)setObject:(id)object
{
    [super setObject:object];
    
}
- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.dataSource objectAtIndex:section] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITVCell_base *cell;
    if (indexPath.section == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell0" forIndexPath:indexPath];
        
            if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
                cell.separatorInset = UIEdgeInsetsMake(0, kScreenWidth/2, 0, kScreenWidth/2);
            }
        
    } else {
        if (indexPath.section == 1 && indexPath.row == 0) {
            cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
            
        } else {
            cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
            
        }
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            cell.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
        }
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        cell.layoutMargins = UIEdgeInsetsZero;
    }
    
    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        cell.preservesSuperviewLayoutMargins = false;
    }
    cell.object = [[self.dataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 && indexPath.row == 0) {
        CourseModel *mo_selected = _orderModel.mo_course;
        if ([CYDataHelper isNull:mo_selected.modelId] || mo_selected.deleted) {
            [self showAlertViewWithTitle:@"抱歉，课程已删除无法购买或学习。如有疑问请咨询客服。"];
            return;
        }

        CourseModel *mo = [[CourseModel alloc]init];
        mo.modelId = _orderModel.mo_course.modelId;
        ECUIVC_CourseDetail *uivc_detail = [[ECUIVC_CourseDetail alloc]init];
        uivc_detail.hidesBottomBarWhenPushed = YES;
        uivc_detail.object = mo;
        [self.navigationController pushViewController:uivc_detail animated:YES];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 45;
    } else {
        if (indexPath.row == 0) {
            return kProportion_Height(246);
        } else {
            return kProportion_Height(144);
        }
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    if (section == 0) {
        view.frame = CGRectMake(0, 0, kScreenWidth, kProportion_Height(24));
    } else {
        view.frame = CGRectMake(0, 0, kScreenWidth, kProportion_Height(30));
 
    }
    view.backgroundColor = [UIColor colorWithHex:0xf5f5f5];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return kProportion_Height(24);
    }
    return kProportion_Height(30);
}
- (void)showAlertViewWithTitle:(NSString *)title
{
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:title attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:18.0f],NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#000000"]}];
    _uiview_alert = [[ECUIAlertView_CourseDetail alloc]initWithTitle:attributeString cancleTitle:@"确定" confirmTitle:  nil];
    [_uiview_alert showAlert];
  
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
